package com.xt.partjobback.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xt.partjobback.constant.CommonConstant;
import com.xt.partjobback.constant.ResultDTO;
import com.xt.partjobback.entity.User;
import com.xt.partjobback.service.UserService;
import com.xt.partjobback.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 小童
 */
@RestController
@RequestMapping(CommonConstant.API_URI_VERSION_V1+"/user")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/login")
    public ResultDTO<String> userLogin(@RequestBody User user){
        return ResultDTO.success(userService.userLogin(user));
    }

    @PostMapping("/register")
    public ResultDTO<String> userRegister(@RequestBody User user){
        return ResultDTO.success(userService.userRegister(user));
    }

    @PostMapping("/update")
    public ResultDTO<Boolean> updateUser(@RequestBody User user){
        return ResultDTO.success(userService.updateUser(user));
    }

    @GetMapping("/delete")
    public ResultDTO<Boolean> deleteUser(Integer id){
        return ResultDTO.success(userService.deleteUser(id));
    }

    @PostMapping("/getUser")
    public ResultDTO<Page<User>> getUser(@RequestBody User user){
        return ResultDTO.success(userService.getUserByCondition(user));
    }

    @GetMapping("/getUserById")
    public ResultDTO<User> getUserById(String id){
        return ResultDTO.success(userService.getById(TokenUtil.tokenExtractId(id)));
    }
}
